<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Comments/classes/PhpFox_ComponentAddForm.class.php, $Author:  $ */


/** Component for viewing add form for comments
 *
 * @version $Id: PhpFox_ComponentAddForm.class.php 440 2008-04-18 19:00:12Z phpfoxsv $
 * @package module.comments
 */
class PhpFox_Mod_Comments_ComponentAddForm extends BaseComponent
{
    function process()
    {
        if ( !App::getUser('user') )
        {
        	return;
        }
    	
    	$oCommentItem = &$this->_oModule->getItem('Comment');
        /* @var $oCommentItem PhpFox_Mod_Comments_ItemComment */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oAccountService = &App::getModuleService('Account', 'Account');
        /* @var $oAccountService PhpFox_Mod_Account_ServiceAccount */
        $oBlockService = &App::getModuleService('Account', 'Block');
        /* @var $oBlockService PhpFox_Mod_Account_ServiceBlock */
        $oCommentService = &$this->_oModule->getService('Comment');
        /* @var $oCommentService PhpFox_Mod_Event_ServiceComment */
        $oTpl = &$this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        App::loadClass('Text');
        $oTxt = &new Text();
		
		$iAlertId = $this->getParam('alert_id');
        $sUser = $oSecurityService->getCurrentUserLogin();
        $oCurrentUser = $oSecurityService->getCurrentUser();
        $aTypes = array
        (
            'blog',
            'gallery',
            'account',
            'music_album',
            'music_song',
            'video',
        );
        $sType = $this->_aParams['type'];
        if (!in_array($sType, $aTypes))
        {
            return '';
        }
        $mId = $this->getParam('id');
        if (!$mId)
        {
            if ($sType == 'account')
            {
                $mId = $oReq->get('name');
            }
            else
            {
                $mId = $oReq->getInt('id');
            }
        }
        if (!$mId)
        {
            return '';
        }

        switch ($sType)
        {
            case 'blog':
                if (!$oSecurityService->checkPerm('blog', array('read_only', 'full'), 0))
                {
                    return '';
                }
                break;
            case 'gallery':
                if (!$oSecurityService->checkPerm('gallery', array('read_only', 'full'), 0))
                {
                    return '';
                }
                break;
            case 'account':
                if (!$oSecurityService->checkPerm('profile', array('read_only', 'full'), 0))
                {
                    return '';
                }
                break;
            case 'music_album':
            case 'music_song':
                if (!$oSecurityService->checkPerm('music', array('read_only', 'full'), 0))
                {
                    return '';
                }
                break;
        }

        $aValues = array();
        if ($oReq->getAction() == 'comment')
        {
            $aValues = $oReq->getArray('val');

            if (!$oCommentService->checkFlood())
            {
                $this->addErrors(App::format('Mod_Comments.flooded', App::getSetting('flood_time')));
            }
            if (!$aValues['text'])
            {
                $this->addErrors(App::format('Mod_Comments.error.no_text'));
            }
            if (empty($aValues['alert_id']))
            {
                $this->addErrors(App::format('Mod_Comments.error.no_alert_id'));
            }
            if (!count($this->getErrors()))
            {
                $sOwner = '';
                $sCommentUrl = '';
                switch ($sType)
                {
                	case 'video':
                	$oAccountService->changeUserInfo($oCurrentUser, 'comment');
					$oCommentService->addComment(App::getParam('aCommentId',$sType), $mId, App::getUser('id'), $oTxt->preParseText($aValues['text']), $iAlertId);
                	break;
                    case 'blog':
                        $oBlogItem = &App::getModuleItem('Blog', 'Blog');
                        /* @var $oBlogItem PhpFox_Mod_Blog_ItemBlog */
                        $sCommentUrl = App::makeUrl('public.blog.view', array('id' => $mId));
                        if ($oBlogItem->load($mId))
                        {
                            $sOwner = $oBlogItem->aData['user'];
                            $oOwner = $oSecurityService->getUserByName($sOwner);
                            if (!$oBlockService->isBlocked($oOwner, $oCurrentUser))
                            {
                                $oAccountService->changeUserInfo($oCurrentUser, 'comment');
                                $oCommentService->addComment(App::getParam('aCommentId',$sType), $mId, App::getUser('id'), $oTxt->preParseText($aValues['text']), $iAlertId);
                            }
                            else
                            {
                                $this->addErrors(App::format('Mod_Comments.error.blocked'));
                            }
                        }
                        break;
                    case 'gallery':
                        $oGalleryItem = &App::getModuleItem('Gallery', 'Gallery');
                        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
                        $sCommentUrl = App::makeUrl('public.gallery.view', array('id' => $mId));
                        if ($oGalleryItem->load($mId))
                        {
                            $sOwner = $oGalleryItem->aData['user'];
                            $oOwner = $oSecurityService->getUserByName($sOwner);
                            if (!$oBlockService->isBlocked($oOwner, $oCurrentUser))
                            {
                                $oCommentService->addComment(App::getParam('aCommentId',$sType), $mId, App::getUser('id'), $oTxt->preParseText($aValues['text']), $iAlertId);
                                $oAccountService->changeUserInfo($oCurrentUser, 'comment');
                                $oCommentService->updateCounts('main', 'comment', "id = '". $mId ."'", App::getParam('aCommentId',$sType), $mId);
                            }
                            else
                            {
                                $this->addErrors(App::format('Mod_Comments.error.blocked'));
                            }
                        }
                        break;
                    case 'account':
                        $oAccountService = &App::getModuleService('Account', 'Account');
                        /* @var $oAccountService PhpFox_Mod_Account_ServiceAccount */
                        $oFriendService = &App::getModuleService('Account', 'Friends');
                        /* @var $oFriendService PhpFox_Mod_Account_ServiceFriends */
                        
                        $oAcc = '';
                        if ($oAcc = $oAccountService->getByUsername($mId))
                        {
                            $sCommentUrl = App::makeUrl('public.user', array('name' => $oAcc->aData['user']));
                        	
                        	if (($oAcc->aData['user'] != $oCurrentUser->aData['user']) && !$oFriendService->isFriend($oCurrentUser, $oAcc) && ($oAcc->aData['friends_comment']))
                            {
                                $this->addErrors(App::format('Mod_Comments.not_friend', $oAcc->aData['user']));
                            }

							if ( !App::getParam('profile_user_ocmnt') && $mId == App::getUser('id'))
							{
								$this->addErrors(App::format('Mod_Comments.no_can_do'));
							}

                            if (!count($this->getErrors()))
                            {
                                $bIsGb = $this->getParam('gb');
								$sNewUrl = (empty($bIsGb) ? App::makeUrl('public.user', array('name' => $oAcc->aData['user'])) : '');
                                $sOwner = $oAcc->aData['user'];
                                $oOwner = $oSecurityService->getUserByName($sOwner);
                                if (!$oBlockService->isBlocked($oOwner, $oCurrentUser))
                                {
                                    $oCommentService->addComment(App::getParam('aCommentId',$sType), $oAcc->aData['id'], App::getUser('id'), $oTxt->preParseText($aValues['text']), $iAlertId);
                                    $oAccountService->changeUserInfo($oCurrentUser, 'comment');
                                    $oCommentService->updateCounts('user', 'c_comments', "id = '". $oAcc->aData['id'] ."'", App::getParam('aCommentId',$sType), $oAcc->aData['id']);
                                }
                                else
                                {
                                    $this->addErrors(App::format('Mod_Comments.error.blocked'));
                                }
                            }
                        }
                        break;
                    case 'music_album':
                        $oAlbumItem = &App::getModuleItem('Music', 'Album');
                        /* @var $oSongItem PhpFox_Mod_Music_ItemAlbum */

                        $sCommentUrl = App::makeUrl('public.music.album', array('id' => $mId));
                        if ($oAlbumItem->load($mId))
                        {
                            $oOwner = $oAccountService->getById($oAlbumItem->aData['user_id']);
                            if (!$oBlockService->isBlocked($oOwner, $oCurrentUser))
                            {
                                $oAccountService->changeUserInfo($oCurrentUser, 'comment');
                                $oCommentService->addComment(App::getParam('aCommentId',$sType), $mId, App::getUser('id'), $oTxt->preParseText($aValues['text']), $iAlertId);
                            }
                            else
                            {
                                $this->addErrors(App::format('Mod_Comments.error.blocked'));
                            }
                        }
                        break;
                    case 'music_song':
                        $oSongItem = &App::getModuleItem('Music', 'Song');
                        /* @var $oSongItem PhpFox_Mod_Music_ItemSong */

                        $sCommentUrl = App::makeUrl('public.music.song', array('id' => $mId));
                        if ($oSongItem->load($mId))
                        {
                            $oOwner = $oAccountService->getById($oSongItem->aData['user_id']);
                            if (!$oBlockService->isBlocked($oOwner, $oCurrentUser))
                            {
                                $oAccountService->changeUserInfo($oCurrentUser, 'comment');
                                $oCommentService->addComment(App::getParam('aCommentId',$sType), $mId, App::getUser('id'), $oTxt->preParseText($aValues['text']), $iAlertId);
                            }
                            else
                            {
                                $this->addErrors(App::format('Mod_Comments.error.blocked'));
                            }
                        }
                        break;
                }
                if (!count($this->getErrors()) && $sOwner && ($sOwner != $sUser) && ($oOwner->aData['not_1'] != 0))
                {
                    $oMailer = &App::newObject('mail_Mailer');
                    /* @var $oMailer mail_Mailer */
                    $sSubject = App::format('core.comment');
                    $sTo = $oOwner->aData['email'];
                    $aVars = array
                    (
                        'TO'           => $sOwner,
                        'FROM'         => $sUser,
                        'SITENAME'     => App::getParam('url'),
                        'HOME'         => App::getParam('url'),
                        'COMMENT_LINK' => $sCommentUrl,
                    );
                    $oMailer->send('email_comment', $sTo, $sSubject, $aVars);
                }
                $oUrl->clearParams('cid');
                $oUrl->clearParams('type');

                App::forward((empty($sNewUrl) ? $oUrl->getUrl() : $sNewUrl), $this->getErrors(), 'error', App::getPageName(), 'add-comment', 'comment');
            }
        }
        
        if (($iQuoteId = $oReq->get('quote')) && is_numeric($iQuoteId))
        {
        	if ($sType == 'video')
        	{
        		$aRow = $this->oDatabase->getRow("
        			SELECT
        				c.cm_text AS text, u.user
        			FROM " . App::getT('videos_comments') . " AS c
        				JOIN " . App::getT('mail_alert') . " AS a ON(c.cm_id = a.c_id)
        				LEFT JOIN " . App::getT('user') . " AS u ON(c.cm_userid = u.id)
        			WHERE c.cm_id = '" . $iQuoteId . "'
        		");
        	}
        	else
        	{
        		$aRow = $this->oDatabase->getRow("
        			SELECT
        				c.text, u.user
        			FROM " . App::getT('comments') . " AS c
        				JOIN " . App::getT('mail_alert') . " AS a ON(c.cid = a.c_id)
        				LEFT JOIN " . App::getT('user') . " AS u ON(c.userid = u.id)
        			WHERE c.cid = '" . $iQuoteId . "'
        		");
        	}
        	App::loadClass('Text');
        	$oTxt = new Text;
        	$aValues['text'] = '[quote="' . $aRow['user'] . '"]' . $oTxt->parseText($aRow['text']) . '[/quote]';	
        }
        

        $oTpl->assign(array(
        	'iAlertId' => $iAlertId,
            'aValues'      => $aValues,
            'bLogged'      => ($sUser) ? true : false,
            'sCommentType' => $sType,
            'aErrors2'     => $this->getErrors(),
            'mId'          => $mId,
            'isuser' => $this->getParam('isuser'),
            'useAjax' => App::getParam('useAjax'),
        ));

        return $oTpl->fetch('AddForm.html');
    }
}
?>